/**
 * FileName: CheckNull Author:   baoy Date:     2018/10/12 11:54 Description:
 */
package com.baoy.xxx.guava.preconditions;

import static com.google.common.base.Preconditions.*;

import com.google.common.primitives.Ints;
import java.util.List;

public class CheckNull {


    public static void main(String[] arg) {
        int i = 1;
        String a = "";
        int j[] = {4, 1, 2, 3};
        List l = Ints.asList(j);

        //前置检查方法
        //条件检查 不通过抛出IllegalArgumentException及自定义描述 自定义描述支持类似于String.format()的字符串拼接但是只能用%s
        checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
        //空值检查 不通过抛出NullPointerException及自定义描述
        checkNotNull(a, "Argument was null");
        //状态检查 不通过抛出IllegalStateException及自定义描述
        checkState(i >= 0, "Argument was %s but expected nonnegative", i);
        //检查列表、字符串或数组某一索引是否有效 不通过抛出IndexOutOfBoundsException
        checkElementIndex(2, l.size());
        //检查列表、字符串或数组某一位置是否有效 不通过抛出IndexOutOfBoundsException
        checkPositionIndex(2, l.size());
        //检查列表、字符串或数组某一范围是否有效 不通过抛出IndexOutOfBoundsException
        checkPositionIndexes(1, 4, l.size());
    }
}
